# -*- coding: utf-8 -*-
# @Date    : 2015-06-21 15:50:35
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

from lib.tornado_routes import route
from web.handler import BaseHandler
from tornado import gen
from tornado.web import asynchronous
from decorator.auth import admin_login_required, admin_auth
import time
from web.models.admin.food.outlog import Outlog
from lib.libpage import start as pagestart
import json
import md5
import base64
@route(r'/admin/food/outlog/?',name='food.outlog')
class IndexHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        model = Outlog()
        cur_page = self.get_argument('page', default=1)
        start = pagestart(cur_page, self.offset)     
        end=self.offset
        rest = yield model.list(start, end)

        ret=rest['ret']
        count=rest['count']['count']

        page_info={'total':int(count), 'page':int(cur_page)}
        self.render("admin/food/outlog/list.html",page_info=page_info,restlist=ret)


#处理搜索
@route(r'/admin/food/outlogsearch/?')
class IndexsearchHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        model = Outlog()
        Fname=self.get_argument('Fname',default='')
        Ftel=self.get_argument('Ftel',default=1)
        Fstatus=self.get_argument('Fstatus',default=1)
        cur_page = self.get_argument('page', default=1)
        start = pagestart(cur_page, self.offset)
        end=self.offset

        rest=yield model.listsr(start,end,Fname,Ftel,Fstatus)
        if rest !=-1:
            ret=rest['ret']
            count=rest['count']['count']
        
            page_info={'total':int(count), 'page':int(cur_page)}
            self.render("admin/food/outlog/list.html",page_info=page_info,restlist=ret)
        else:
            self.redirect("/admin/food/outlog/")

@route(r'/admin/food/outlog/new/?')
class NewHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        self.render("admin/food/outlog/new.html",)



#处理记录状态
@route(r'/admin/food/outlog/status/(?P<Fid>[0-9].*)/(?P<Fstatus>[0-9].*)/(?P<cur_page>[0-9].*)')
class StatusHandler(BaseHandler):
    @asynchronous
    @gen.coroutine
    def get(self):
        model = Outlog()
        Fstatus=self.get_argument('Fstatus',default=1)
        Fid=self.get_argument('Fid',default=1)
        rest=yield model.status(Fid,Fstatus)

        if rest:
             self.redirect("/admin/food/outlog/")

#批量派送批量完成
@route(r'/admin/food/istatus/(?P<cur_page>[0-9].*)/(?P<Fstatus>[0-9].*)', name='food.set_istatus')
class IstatusHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def post(self, cur_page, Fstatus):
        model=Outlog()
        #school_id = self.get_argument('schoolId', default=self.school_id)
        data = self.get_argument('data', default='')

        str2=str(data)
        tuple2=tuple(str2.split(','))
        
        ret = yield model.set_istatus2(Fstatus,tuple2)
       
        self.write(str(ret))
        
